<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Eventos extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('eventos_model');
		$this->load->library('session');
		$this->load->library('excel');
		$this->load->library('tank_auth');
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->helper('text');
		
		// prevent caching
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		
		// if no session, redirect to login page		
		if(!$this->tank_auth->is_logged_in())
		{
			redirect('admin/login', 'refresh');
		}
	}
	
	public function index()
	{
		if($this->session->flashdata('message'))
		{
			$data['message'] = $this->session->flashdata('message');
		}
		$data['result'] = $this->eventos_model->get();		
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/eventos', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function registro($id = FALSE)
	{				
		// This method will have the credentials validation
		$this->load->library('form_validation');		
		$this->form_validation->set_rules('name', 'Titulo', 'trim|required|xss_clean');
		$this->form_validation->set_rules('shortdesc', 'Descripción corta', 'trim|required|xss_clean');
		$this->form_validation->set_rules('event_date', 'Fecha', 'trim|xss_clean');
		$this->form_validation->set_rules('time', 'Hora', 'trim|xss_clean');
		$this->form_validation->set_rules('price', 'Precio', 'trim|xss_clean|numeric');
		$this->form_validation->set_rules('vacants', 'Vacantes', 'trim|xss_clean|is_natural');
		$this->form_validation->set_rules('mediumdesc', 'Descripción mediana', 'trim|required|xss_clean');
		$this->form_validation->set_rules('body', 'Cuerpo de texto', 'trim');
		$this->form_validation->set_rules('deleted', 'Publicar', 'trim|xss_clean|is_natural|less_than[2]');
		
		if($this->form_validation->run())
		{
			$input = $this->input->post();
			if($this->input->post('deleted') === FALSE)
			{
				$input['deleted'] = 1;
			}
			$defaults = array('event_date'=>NULL, 'time'=>NULL, 'price'=>NULL, 'vacants'=>0, 'body'=>NULL);
			foreach($input as $key=>$value)
			{
				if($this->input->post($key) === '')
				{
					$input[$key] = $defaults[$key];
				}
			}
			
			if($id !== FALSE)
			{
				$this->eventos_model->update($input, $id);
				$this->session->set_flashdata('message', 'Se ha modificado un registro');
			}
			else
			{
				$this->eventos_model->insert($input);
				$this->session->set_flashdata('message', 'Se ha insertado un registro');
			}			
			redirect('admin/eventos', 'refresh');
		}
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		if($id !== FALSE)
		{
			$data['row'] = $this->eventos_model->get_id($id);
		}
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/eventos-registro', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function eliminar($id)
	{		
		$this->eventos_model->delete($id);
		
		$this->session->set_flashdata('message', 'Se ha eliminado un registro');
		redirect('admin/eventos', 'refresh');
	}
	
	public function inscritos($id)
	{				
		$data['row'] = $this->eventos_model->get_id($id);
		$data['result'] = $this->eventos_model->get_inscritos($id);	
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/eventos-inscritos', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function exportar($id = FALSE)
	{
		if($id === FALSE)
		{
			$result_evento = $this->eventos_model->get();
		}
		else
		{
			$result_evento[0] = $this->eventos_model->get_id($id);
			$inscritos = $this->eventos_model->get_inscritos($id);
		}
		
		// create worksheets
		for($i = 0; $i < count($result_evento); $i++)
		{
			if($i > 0)
			{
				$this->excel->createSheet();
			}
			$title = strlen($result_evento[$i]->name) < 20 ? $result_evento[$i]->name : substr($result_evento[$i]->name, 0, 20).'...'; 
			$this->excel->getSheet($i)->setTitle($title);			
			$this->excel->getSheet($i)->setCellValue('A1', $result_evento[$i]->name);
			$this->excel->getSheet($i)->setCellValue('A2', 'Nombres');
			$this->excel->getSheet($i)->setCellValue('B2', 'Apellidos');
			$this->excel->getSheet($i)->setCellValue('C2', 'Documento de identidad');
			$this->excel->getSheet($i)->setCellValue('D2', 'Email');
			$this->excel->getSheet($i)->setCellValue('E2', 'Celular');
			$this->excel->getSheet($i)->setCellValue('F2', 'Fecha');
			$this->excel->getSheet($i)->mergeCells('A1:F1');
			$this->excel->getSheet($i)->getStyle('A1:F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
			$this->excel->getSheet($i)->getStyle('A1:F2')->getFont()->setBold(true);
			foreach(range('A', 'F') as $columnID) {
				$this->excel->getSheet($i)->getColumnDimension($columnID)->setAutoSize(true);
			}
			
			if($id === FALSE)
			{
				$inscritos = $this->eventos_model->get_inscritos($result_evento[$i]->id);
			}
			$j = 2;
			foreach($inscritos as $row)
			{
				$this->excel->getSheet($i)->setCellValue('A'.$j, $row->name);
				$this->excel->getSheet($i)->setCellValue('B'.$j, $row->lname);
				$this->excel->getSheet($i)->setCellValue('C'.$j, $row->dni);
				$this->excel->getSheet($i)->setCellValue('D'.$j, $row->email);
				$this->excel->getSheet($i)->setCellValue('E'.$j, $row->cel);
				$this->excel->getSheet($i)->setCellValue('F'.$j, $row->date);
				$j++;
			}
		}
			
		$this->excel->setActiveSheetIndex(0);
		
		$filename='starscamp_eventos_inscritos.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}
}

/* End of file eventos.php */
/* Location: ./application/controllers/admin/eventos.php */